//
//  HomeScreen.swift
//  swiftUIdemo
//
//  Created by Z.cy on 2025/1/8.
//

import SwiftUI

struct HomeScreen: View {
    @EnvironmentObject var appState : AppTabBarState
    @StateObject var viewModel = HomeViewModel()
    
    var body: some View {
        VStack(spacing: 0){
            if !appState.isTabBarHidden{
                HStack{
                    HomeTitleButton(selectedTitle: $viewModel.selectedTitle, title: .TanTan)
                    HomeTitleButton(selectedTitle: $viewModel.selectedTitle, title: .PICK)
                    Spacer()
                }
                .padding([.leading, .top],15)
            }
            switchTab()
            
            
        }
    }
    
    @ViewBuilder
    func switchTab() -> some View {
        switch viewModel.selectedTitle {
        case .TanTan:
            if viewModel.hasMoreCard {
                ZStack{
                    ForEach( viewModel.displayingCard.reversed()){card in
                        CardContainerView(viewModel: viewModel, card: card).environmentObject(appState)
                    }
                }
            }else{
                // 没有更多的卡片此处应该是一个空的占位视图
            }
        case .PICK:
            PickListView(cards: viewModel.displayingCard)
        }
    }
    
    
    
    enum HomeTitle:String {
    case TanTan
    case PICK
    }
    
    
    
    
}
struct HomeTitleButton:View {
    @Binding var selectedTitle :HomeScreen.HomeTitle
    var title :HomeScreen.HomeTitle
    var body: some View {
        Button{
            selectedTitle = title
            
        } label: {
            Text(title.rawValue)
                .font(.system(size: 25))
                .fontWeight(.bold)
                .foregroundColor(selectedTitle == title ? .accentColor: .gray.opacity(0.6))
        }
        
    }
}

struct HomeScreen_Previews: PreviewProvider {
    static var previews: some View {
         HomeScreen()
            .environmentObject(AppTabBarState())
    }
}
